package com.yunfei.springbootgoods.controller;

import com.yunfei.springbootgoods.dto.Page;
import com.yunfei.springbootgoods.dto.Result;
import com.yunfei.springbootgoods.entity.ShoppingCar;
import com.yunfei.springbootgoods.exception.MyException;
import com.yunfei.springbootgoods.exceptionEnums.ExceptionEnum;
import com.yunfei.springbootgoods.service.GoodsOrderService;
import com.yunfei.springbootgoods.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@CrossOrigin
@RestController
public class ShoppingCarController {

    @Autowired
    private ShoppingCarService shoppingCarService;

    @Autowired
    private GoodsOrderService goodsOrderService;

    /**
     * 获取购物车分页列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/getShoppingCarPage")
    public Result getShoppingCarPage(Integer pageNo, Integer pageSize){
        try {
            Page shoppingCarPage = shoppingCarService.getShoppingCarPage(pageNo, pageSize);
            Result result = new Result();
            result.setData(shoppingCarPage);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.ERROR_CAR_LIST);
        }
    }

    /**
     * 通过购物车ID删除信息
     * @param carId
     * @return
     */
    @GetMapping(value = "/deleteShoppingCar")
    public Result deleteShoppingCar(Integer carId){
        try {
            shoppingCarService.deleteShoppingCar(carId);
            Result result = new Result();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.ERROR_CAR_DELETE);
        }

    }

    @GetMapping(value = "/addGoodsOrder")
    public Result addGoodsOrder(Integer carId){

        try {
            ShoppingCar shoppingCarById = shoppingCarService.getShoppingCarById(carId);
            System.out.println(shoppingCarById);
            //System.out.println(shoppingCarById.getGoodsId());
            goodsOrderService.addGoodsOrder(shoppingCarById.getGoodsId());

            shoppingCarService.deleteShoppingCar(carId);

            Result result = new Result();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.ERROR_ORDER_ADD);
        }


    }
}



















